package handler

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/sirupsen/logrus"
	"honghu_be/common"
	"honghu_be/dal"
	"honghu_be/model"
	"honghu_be/model/vo"
)

func CreateArticle(c *gin.Context) {
	req := &model.CreateArticleRequest{}
	if err := c.ShouldBindBodyWith(req, binding.JSON); err != nil {
		c.JSON(400, err.Error())
		return
	}
	logrus.Info("Some called GetArticles, req is %+v", req)

	resp, err := createArticle(c, req)
	if err != nil {
		common.WriteError(c, err)
		return
	}
	c.JSON(200, resp)
}

func createArticle(c *gin.Context, req *model.CreateArticleRequest) (*model.CreateArticleResponse, error) {
	po := req.Article.ToPo()
	err := dal.CreateArticle(c, po)
	if err != nil {
		return nil, err
	}
	return &model.CreateArticleResponse{
		Meta:    common.MetaOk,
		Article: vo.ToArticleVo(po),
	}, nil
}
